package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Transformations.switchMap
import com.cheyun.netsalev3.bean.RowX
import com.cheyun.netsalev3.repository.MessageListRepository
import com.cheyun.netsalev3.utils.MyIntent
import com.cheyun.netsalev3.view.ClueListActivity
import com.cheyun.netsalev3.view.CuesAllocationActivity
import com.cheyun.netsalev3.view.PassengerRegisterActivity
import com.cheyun.netsalev3.view.allocated.AllocatedActivity
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity
import com.cheyun.netsalev3.widget.MyContentDialog

class MessageListActivityViewModel(var messageListRepository: MessageListRepository) : BaseViewModel() {

    var listData=messageListRepository.loadAndroidData()
    var networkState = switchMap(listData) {
        messageListRepository.networkState
    }!!
    var total = messageListRepository.total

    fun refresh() {
        messageListRepository.dataSourceFactory.sourceLiveData.value?.invalidate()

    }

    fun onClickItem(view: View, item: RowX) {
        if(item.message.contains("即将超时")){
            var intent = Intent(view.context, AllocatedActivity::class.java)
            view.context.startActivity(intent)
            return
        }
        var moth: () -> Unit = {
            item.ckey?.apply {
                var ckeys = this.split(".")
                if (ckeys.size == 2) {
                    when (ckeys[0]) {
                        "archive" -> {
                            if (ckeys[1] == "0") {
                                //跳转线索列表
                                var intent = Intent(view.context, ClueListActivity::class.java)
                                view.context.startActivity(intent)
                            } else {
                                //跳转线索
                                var intent = Intent(view.context, DefeatDetailsActivity::class.java)
                                intent.putExtra("tid", ckeys[1].toInt())
                                intent.putExtra("nkey", "archive.index")
                                view.context.startActivity(intent)
                            }

                        }
                        "receive" -> {
                            //跳转接待  2021-12-01 客流接待换成到店管理
//                            myStartActivity(MyIntent(PassengerReceptionActivity::class.java))
                            myStartActivity(MyIntent(PassengerRegisterActivity::class.java))

                        }
                        "SalesArchiveTBA" ->{
                            myStartActivity(MyIntent(CuesAllocationActivity::class.java))
                        }


                    }
                }
            }
        }
        messageListRepository.getNotice(item.id, moth)
    }

    fun allRead(view:View){
        var content = view.context
        if (content is AppCompatActivity) {
            var pop = MyContentDialog.newInstance("提示", "是否全部设置为已读？")
            pop.show(content.supportFragmentManager, "")
            var moth: () -> Unit = {
                messageListRepository.allRead(this::setData)
            }
            pop.onClickOk = moth
        }
    }

    fun allClear(view:View){
        var content = view.context
        if (content is AppCompatActivity) {
            var pop = MyContentDialog.newInstance("提示", "是否清空消息？")
            pop.show(content.supportFragmentManager, "")
            var moth: () -> Unit = {
                messageListRepository.allClear(this::setData)
            }
            pop.onClickOk = moth
        }
    }

    fun setData() {
        this.refresh()
    }


}